#!/bin/sh



#############################
# function setupPaths {}    #
#                           #
# Set up installation paths #
#############################
function setupPaths {
    # The location all the files are installed to; a temporary location
    # from where all the files are distributed to their appropriate
    # final destination locations.
    TMP_INSTALL_FOLDER="$1";

    TMP_INSTALL_SUBFOLDER_AUS="$TMP_INSTALL_FOLDER""/AUS/";
    TMP_INSTALL_SUBFOLDER_LCC="$TMP_INSTALL_FOLDER""/LCC/";
    TMP_INSTALL_SUBFOLDER_POS="$TMP_INSTALL_FOLDER""/POS/";
    TMP_INSTALL_SUBFOLDER_PKG="$TMP_INSTALL_FOLDER""/PKG/";

    # The paths of this script an its resources.
    PATH_TO_PARENT_FOLDER=`/usr/bin/dirname "$RECEIPT_PATH"`"/";
    PATH_TO_RESOURCES_FOLDER="$PATH_TO_PARENT_FOLDER""Resources/";

    # The appropriate final destination locations of the eLC components.
    # LCC paths.
    DESTINATION_FOLDER_LCC="/Applications/";
    LCC_APP_NAME="License Control Center.app";
    LCC_FULLPATH="$DESTINATION_FOLDER_LCC""$LCC_APP_NAME";

    # POS paths.
    DESTINATION_FOLDER_POS="/Library/Application Support/";
    POS_FULLPATH="$DESTINATION_FOLDER_POS""eLicenser/";
    POS_LEGACY_FULLPATH="$DESTINATION_FOLDER_POS""Syncrosoft/";
    POS_LEGACY_SYMLINK_TARGET="$DESTINATION_FOLDER_POS""Syncrosoft";
    POS_COMPONENTS_PARENT_PATH="$POS_FULLPATH""pos/";
    POS_COMPONENT_POS="$POS_COMPONENTS_PARENT_PATH""Synsopos.app";
    POS_COMPONENT_ACC="$POS_COMPONENTS_PARENT_PATH""Synsoacc.bundle";
    POS_COMPONENT_SEL="$POS_COMPONENTS_PARENT_PATH""SynsoSeL.bundle";

    # SEL paths.
    DESTINATION_FOLDER_SEL="/Library/Application Support/";
    SEL_FULLPATH="$DESTINATION_FOLDER_SEL""Soft-eLicenser/";
    SEL_STORAGE_NAME="SeLicenser.sel";
    SEL_STORAGE_FULLPATH="$SEL_FULLPATH""$SEL_STORAGE_NAME";

    # AUS paths.
    DESTINATION_FOLDER_AUS="/System/Library/Extensions/";
    PATH_TO_AUS_KERNEL_PLUGIN="$DESTINATION_FOLDER_AUS""AuthenticationSupport.plugin";
    PATH_TO_AUE_KERNEL_PLUGIN="$DESTINATION_FOLDER_AUS""AuthenticationSupportEnabler.plugin";

    DESTINATION_FOLDER_KERNEL_PLUGIN_PREFS="/Library/Preferences/";
    AUS_KERNEL_PLUGIN_PREFS_FILE="AuthenticationSupport.plist";
    PATH_TO_AUS_KERNEL_PLUGIN_PREFERENCES="$DESTINATION_FOLDER_KERNEL_PLUGIN_PREFS"".$AUS_KERNEL_PLUGIN_PREFS_FILE";

    # Receipts PKG paths.
    DESTINATION_FOLDER_RECEIPTS_PKG="/Library/Receipts/";
    RECEIPTS_PKG_LCC_FULLPATH="$DESTINATION_FOLDER_RECEIPTS_PKG""LCC.pkg";
    RECEIPTS_PKG_NOS_FULLPATH="$DESTINATION_FOLDER_RECEIPTS_PKG""NOS.pkg";
    RECEIPTS_PKG_POS_FULLPATH="$DESTINATION_FOLDER_RECEIPTS_PKG""POS.pkg";
    RECEIPTS_PKG_SLC_FULLPATH="$DESTINATION_FOLDER_RECEIPTS_PKG""SyncrosoftLicenseControl.pkg";

    # Receipts plist paths.
    DESTINATION_FOLDER_RECEIPTS_PLIST="/var/db/receipts/";
    RECEIPTS_PLIST_SLC_FULLPATH="$DESTINATION_FOLDER_RECEIPTS_PLIST""com.syncrosoft.installer.licensecontrol.plist";
    RECEIPTS_BOM_SLC_FULLPATH="$DESTINATION_FOLDER_RECEIPTS_PLIST""com.syncrosoft.installer.licensecontrol.bom";

    # The location of the global log folder used by all components of the eLicenser Control.
    PATH_TO_GLOBAL_LOG_FOLDER="/Library/Logs/eLicenser/";

    # Legacy log file paths.
    PATH_TO_LEGACY_USER_LOG_PARRENT_FOLDER="$HOME/Library/Logs/Syncrosoft/";
    PATH_TO_LEGACY_USER_LOG_FOLDER="$PATH_TO_LEGACY_USER_LOG_PARRENT_FOLDER/LCC/";
    PATH_TO_LEGACY_GLOBAL_LOG_PARRENT_FOLDER="/Library/Logs/Syncrosoft/";
    PATH_TO_LEGACY_SEL_LOG_FILE="$POS_LEGACY_FULLPATH""/pos/Logs/SelInformation.txt";
    PATH_TO_CURRENT_SEL_LOG_FILE="$PATH_TO_GLOBAL_LOG_FOLDER""/POSSeLActionsLog.txt";

    # Symbolic link paths.
    POS_SYMLINK_SOURCE="$POS_COMPONENTS_PARENT_PATH";
    POS_PARENT_FOLDER_OF_SYMLINK_TARGET="$DESTINATION_FOLDER_POS""Syncrosoft/";
    POS_SYMLINK_TARGET="$POS_PARENT_FOLDER_OF_SYMLINK_TARGET""pos";

    # The location of the Launchservices tool we want to call
    # to ensure Synsopos runs natively on Intel-Macs.
    LAUNCHSERVICES="/System/Library/Frameworks/ApplicationServices.framework/Versions/Current/Frameworks/LaunchServices.framework/Support/lsregister";

    # The location of the OSAScript tool we need to call to add
    # the Synsopos to the current user's list of LoginItems.
    SCRIPTEXECUTER="/usr/bin/osascript";

    # The location of the AppleScript which adds the Synsopos
    # to the current user's list of LoginItems.
    #APPLESCRIPT_TO_CREATE_POS_LOGINITEM="$PATH_TO_RESOURCES_FOLDER""InstallPOSAsLoginItem.scpt";

    # The platform specific paths.
    PLATFORM_SPECIFIC_AUS_SUB_PATH_PPC="/AUS_ppc";
    PLATFORM_SPECIFIC_AUS_SUB_PATH_I386="/AUS_i386";
    PATH_TO_PLATFORMINFO_TOOL="$PATH_TO_RESOURCES_FOLDER""i386orPPC";

    # LCC Launcher paths.
    PATH_TO_LCC_LAUNCHER_ROOT_FOLDER="$PATH_TO_RESOURCES_FOLDER""LCC_Launcher/";
    PATH_TO_LCC_LAUNCHER="$PATH_TO_LCC_LAUNCHER_ROOT_FOLDER""LCC Launcher.app";
    PATH_TO_LCC_LAUNCHER_FLAG_FILE="$PATH_TO_LCC_LAUNCHER_ROOT_FOLDER"".PPCFixDetected";
    
    # Set default value of global variable.
    RESTORE_SEL_LOG_FROM_TMP_BACKUP=0;
} # setupPaths
###############



##################################
# function checkPlatform {}      #
#                                #
# Determine the current platform #
##################################
function checkPlatform {
    /bin/echo "Determining platform..."
    # Determine platform we're running on.
    # 1: little endian
    # 2: big endian
    PLATFORM=`"$PATH_TO_PLATFORMINFO_TOOL"`;

    if [ $PLATFORM -eq 1 ]; then
        # We're on little endian system.
        PLATFORM_SPECIFIC_AUS_SUB_PATH="$PLATFORM_SPECIFIC_AUS_SUB_PATH_I386";
        /bin/echo "... current platform is Intel Mac.";
    else
        if [ $PLATFORM -eq 2 ]; then
            # We're on big endian system.
           PLATFORM_SPECIFIC_AUS_SUB_PATH="$PLATFORM_SPECIFIC_AUS_SUB_PATH_PPC";
            /bin/echo "... current platform is PowerPC Mac.";
        else
            /bin/echo "ERROR: Could not determine platform!";
            exit 1;
        fi
    fi

    /bin/echo " "
} # checkPlatform
##################



####################################
# function setReceiptsHandling {}  #
#                                  #
# Determine the current platform's #
# Receipts file handling mechanism #
####################################
function setReceiptsHandling {
    /bin/echo "Determining receipts file handling..."

    installerAppUsesReceiptsPackages;
    INSTALLER_SUPPORTS_RECEIPTS_PACKAGES=$?;

    if [ $INSTALLER_SUPPORTS_RECEIPTS_PACKAGES -eq 1 ]; then
        /bin/echo "... current system supports Installer Receipts packages in \"/Library/Receipts/\".";
    else
        /bin/echo "... current system supports Installer Receipts packages in \"/var/db/receipts/\".";
    fi

    /bin/echo " "
} # setReceiptsHandling
########################



##################################
# function checkSeLVersion {}    #
#                                #
# Check for existing SeL version #
##################################
function checkSeLVersion {
    # To identify whether Soft-eLicenser with erroneous serial number is present,
    # we have to check for several conditions:
    # - platform is PPC
    # - Soft-eLicenser has been created already
    # - current SeL version is one of the following:
    #  - "1.0.1.0"
    #  - "1.0.1.1"
    SN_UPDATE_REQUIRED=0;
    if [ $PLATFORM -eq 2 ]; then
        # We're on big endian system.
        
        if [ -f "$SEL_STORAGE_FULLPATH" ]; then
            # Soft-eLicenser storage file exists.
            
            SEL_PLIST="$POS_COMPONENT_SEL""/Contents/Info.plist";
            if [ -f "$SEL_PLIST" ]; then
                # Soft-eLicenser DLL exists.
                # Get current SeL version:
                SEL_VERSION=`/usr/bin/grep -A 1 "<key>CFBundleVersion</key>" "$SEL_PLIST" | /usr/bin/grep "string" | /usr/bin/sed -e s-.*\<string\>-- | /usr/bin/sed -e s-\</string\>.*--`;
                RETURN_VALUE="$?";
                if [ "$RETURN_VALUE" != 0 ]; then
                    /bin/echo "ERROR: Could not determine current SeL version.";
                    /bin/echo " "
                    exit 1;
                fi
            else
                SEL_VERSION="n/a";
            fi

            # Find out whether the existing Soft-eLicenser was one of the erroneous versions.
            ERRONEOUS_SEL_VERSION_01="1.0.1.0";
            ERRONEOUS_SEL_VERSION_02="1.0.1.1";

            if [ "$SEL_VERSION" = "$ERRONEOUS_SEL_VERSION_01" ]; then
                # Need to update serial number.
                SN_UPDATE_REQUIRED=1;
            fi

            if [ "$SEL_VERSION" = "$ERRONEOUS_SEL_VERSION_02" ]; then
                # Need to update serial number.
                SN_UPDATE_REQUIRED=1;
            fi
        fi
    fi

    if [ $SN_UPDATE_REQUIRED -eq 1 ]; then
        # Configure LCC to show dialog for fixed PPC SeL.
        /bin/echo "Detected invalid Soft-eLicenser serial number (due to bug in PPC SeL version);"
        /bin/echo "launching LCC will fix this issue."
        /bin/echo " "
    fi
} # checkSeLVersion
####################



################################################
# function installerAppUsesReceiptsPackages {} #
#                                              #
# Determine the current system version         #
################################################
function installerAppUsesReceiptsPackages {
    RET_VAL=0;

    # Check for minimum system version
    if [ ! -f "/usr/bin/sw_vers" ]; then
        # The system version tool is not present, so we're either on
        # a very old or a very new system, which are both not supported.
        echo "ERROR: Unsupported system version."
        exit 1;
    else
        SYSTEM_VERSION_IS_JAGUAR=`/usr/bin/sw_vers | grep "ProductVersion" | /usr/bin/grep -c "10.2"`;
        SYSTEM_VERSION_IS_PANTHER=`/usr/bin/sw_vers | grep "ProductVersion" | /usr/bin/grep -c "10.3"`;
        SYSTEM_VERSION_IS_TIGER=`/usr/bin/sw_vers | grep "ProductVersion" | /usr/bin/grep -c "10.4"`;
        SYSTEM_VERSION_IS_LEOPARD=`/usr/bin/sw_vers | grep "ProductVersion" | /usr/bin/grep -c "10.5"`;

        if [ "$SYSTEM_VERSION_IS_JAGUAR" -eq 1 ]; then
            echo "ERROR: Unsupported system version."
            exit 1;
        elif [ "$SYSTEM_VERSION_IS_PANTHER" -eq 1 ]; then
            RET_VAL=1;
        elif [ "$SYSTEM_VERSION_IS_TIGER" -eq 1 ]; then
            RET_VAL=1;
        elif [ "$SYSTEM_VERSION_IS_LEOPARD" -eq 1 ]; then
            RET_VAL=1;
        else
            # We're on systems later than Leopard (e.g. Snow Leopard) which uses "/var/db/receipts/" 
            # instead of "/Library/Receipts/".
            RET_VAL=0;
        fi
    fi
    
    return $RET_VAL;
} # installerAppUsesReceiptsPackages
#######################



###########################################
# function migrate_SeL_LogFile_prepare {} #
#                                         #
# Move SeL log file to a                  #
# temporary backup location               #
###########################################
function migrate_SeL_LogFile_prepare {
    # Check for existing SeL log file.
    if [ -f "$PATH_TO_LEGACY_SEL_LOG_FILE" ]; then

        # Set SeL log file backup paths.
        SEL_LOG_BACKUP_FILENAME=`date "+Soft-eLicenser_log_tmp_backup_20%y-%m-%d_%H-%M-%S"`;
        SEL_LOG_FULLPATH_TMP_BACKUP_SOURCE="$PATH_TO_LEGACY_SEL_LOG_FILE";
        SEL_LOG_FULLPATH_TMP_BACKUP_TARGET="$DESTINATION_FOLDER_POS""$SEL_LOG_BACKUP_FILENAME";

        # Backup SeL storage file
        mv "$SEL_LOG_FULLPATH_TMP_BACKUP_SOURCE" "$SEL_LOG_FULLPATH_TMP_BACKUP_TARGET";

        if [ -e "$SEL_LOG_FULLPATH_TMP_BACKUP_SOURCE" ]; then
            echo "WARNING: Could not move Soft-eLicenser log file from: \"$SEL_LOG_FULLPATH_TMP_BACKUP_SOURCE\" to \"$SEL_LOG_FULLPATH_TMP_BACKUP_TARGET\"."
        else
            RESTORE_SEL_LOG_FROM_TMP_BACKUP=1;
            echo "Moved Soft-eLicenser log file to temporary backup location: \"$SEL_LOG_FULLPATH_TMP_BACKUP_TARGET\"."
            echo "This backup log file will be restored later."
        fi
    fi
} # migrate_SeL_LogFile_prepare
###################



##########################################
# function migrate_SeL_LogFile_finish {} #
#                                        #
# Restore SeL log file from              #
# the temporary backup location          #
##########################################
function migrate_SeL_LogFile_finish {
    # Restoring existing SeL log file.
    if [ $RESTORE_SEL_LOG_FROM_TMP_BACKUP -eq 1 ]; then

        if [ -e "$PATH_TO_CURRENT_SEL_LOG_FILE" ]; then
            echo "WARNING: A new Soft-eLicenser log file does already exist, the temporary (old) Soft-eLicenser log file will be appended to the existing one."
            # Append tmp backup log file to existing log file at original location.
            OLD_LOG_FILE_CONTENT=`more "$SEL_LOG_FULLPATH_TMP_BACKUP_TARGET"`´;
            echo " "                                                                        >> "$PATH_TO_CURRENT_SEL_LOG_FILE";
            echo "========== Old log file discovered during log file migration: ==========" >> "$PATH_TO_CURRENT_SEL_LOG_FILE";
            echo "$OLD_LOG_FILE_CONTENT"                                                    >> "$PATH_TO_CURRENT_SEL_LOG_FILE";
            echo "========================================================================" >> "$PATH_TO_CURRENT_SEL_LOG_FILE";
            echo " "                                                                        >> "$PATH_TO_CURRENT_SEL_LOG_FILE";
            
            rm -f "$SEL_LOG_FULLPATH_TMP_BACKUP_TARGET";
        else
            # Move tmp backup log file to original location.
            mv "$SEL_LOG_FULLPATH_TMP_BACKUP_TARGET" "$PATH_TO_CURRENT_SEL_LOG_FILE";
            
            if [ -e "$SEL_LOG_FULLPATH_TMP_BACKUP_TARGET" ]; then
                echo "WARNING: Soft-eLicenser log file could not be restored from backup path: \"$SEL_LOG_FULLPATH_TMP_BACKUP_TARGET\"."
            else
                echo "Restored Soft-eLicenser log file from temporary backup location: \"$SEL_LOG_FULLPATH_TMP_BACKUP_TARGET\"."
            fi
        fi
    fi
} # migrate_SeL_LogFile_finish
###############################



####################################
# function removeOldComponents {}  #
#                                  #
# Removing existing components     #
####################################
function removeOldComponents {
    echo "Removing old components..."

    # Removing LCC.
    if [ -e "$LCC_FULLPATH" ]; then
        rm -rf "$LCC_FULLPATH";
    fi

    if [ -e "$LCC_FULLPATH" ]; then
        echo "ERROR: Could not remove old version of LCC."
        exit 1;
    fi


    # Removing POS folders.
    if [ -e "$POS_FULLPATH" ]; then
               
        rm -rf "$POS_FULLPATH";
    fi

    if [ -e "$POS_FULLPATH" ]; then
        echo "ERROR: Could not remove old POS folders: \"$POS_FULLPATH\"."
        exit 1;
    fi

    if [ -d "$POS_LEGACY_FULLPATH" ]; then
        # Backup any SeL log file -- it will be moved to the new
        # log folder later after the new location has been created.
        migrate_SeL_LogFile_prepare;

        rm -rf "$POS_LEGACY_FULLPATH";
    fi

    if [ -d "$POS_LEGACY_FULLPATH" ]; then
        echo "ERROR: Could not remove legacy POS folder: \"$POS_LEGACY_FULLPATH\"."
        exit 1;
    fi


    # Removing legacy pkg file ("LCC.pkg").
    if [ -e "$RECEIPTS_PKG_LCC_FULLPATH" ]; then
        rm -rf "$RECEIPTS_PKG_LCC_FULLPATH";
    fi

    if [ -e "$RECEIPTS_PKG_LCC_FULLPATH" ]; then
        echo "ERROR: Could not remove old LCC receipts package: \"$RECEIPTS_PKG_LCC_FULLPATH\"."
        exit 1;
    fi


    # Removing legacy pkg file ("NOS.pkg").
    if [ -e "$RECEIPTS_PKG_NOS_FULLPATH" ]; then
        rm -rf "$RECEIPTS_PKG_NOS_FULLPATH";
    fi

    if [ -e "$RECEIPTS_PKG_NOS_FULLPATH" ]; then
        echo "ERROR: Could not remove old NOS receipts package: \"$RECEIPTS_PKG_NOS_FULLPATH\"."
        exit 1;
    fi


    # Removing legacy pkg file ("POS.pkg").
    if [ -e "$RECEIPTS_PKG_POS_FULLPATH" ]; then
        rm -rf "$RECEIPTS_PKG_POS_FULLPATH";
    fi

    if [ -e "$RECEIPTS_PKG_POS_FULLPATH" ]; then
        echo "ERROR: Could not remove old POS receipts package: \"$RECEIPTS_PKG_POS_FULLPATH\"."
        exit 1;
    fi


    # Removing legacy pkg file ("SyncrosoftLicenseControl.pkg").
    if [ -e "$RECEIPTS_PKG_SLC_FULLPATH" ]; then
        rm -rf "$RECEIPTS_PKG_SLC_FULLPATH";
    fi

    if [ -e "$RECEIPTS_PKG_SLC_FULLPATH" ]; then
        echo "ERROR: Could not remove old SLC receipts package: \"$RECEIPTS_PKG_SLC_FULLPATH\"."
        exit 1;
    fi


    # Removing legacy plist file ("com.syncrosoft.installer.licensecontrol.plist").
    if [ -e "$RECEIPTS_PLIST_SLC_FULLPATH" ]; then
        rm -rf "$RECEIPTS_PLIST_SLC_FULLPATH";
    fi

    if [ -e "$RECEIPTS_PLIST_SLC_FULLPATH" ]; then
        echo "ERROR: Could not remove old SLC receipts plist file: \"$RECEIPTS_PLIST_SLC_FULLPATH\"."
        exit 1;
    fi


    # Removing legacy bom file ("com.syncrosoft.installer.licensecontrol.bom").
    if [ -e "$RECEIPTS_BOM_SLC_FULLPATH" ]; then
        rm -rf "$RECEIPTS_BOM_SLC_FULLPATH";
    fi

    if [ -e "$RECEIPTS_BOM_SLC_FULLPATH" ]; then
        echo "ERROR: Could not remove old SLC receipts bom file: \"$RECEIPTS_BOM_SLC_FULLPATH\"."
        exit 1;
    fi

    /bin/echo "... ready removing old components."
    /bin/echo " "
} # removeOldComponents
########################



####################################
# function installLCCComponents {} #
#                                  #
# Copy new LCC components to their #
# correct destination locations    #
####################################
function installLCCComponents {
    # Installing "/Applications/License Control Center.app".
    /bin/echo "Installing LCC components..."

    if [ ! -d "$LCC_FULLPATH" ]; then
        # LCC file/folder has been deleted successfully in the preflight script,
        # so move the new one from the tmp install location to the final destination.
        /bin/mv -f "$TMP_INSTALL_SUBFOLDER_LCC""$LCC_APP_NAME" "$LCC_FULLPATH";
    else
        # LCC file/folder already exists, because it couldn't be deleted in the preflight script.
        /bin/echo "ERROR: Old file could not be removed: ""$DESTINATION_FOLDER_LCC""$LCC_APP_NAME"
        exit 1;
    fi


    if [ ! -d "$LCC_FULLPATH" ]; then
        # LCC file/folder doesn't exist, it hasn't been moved from the tmp location.
        /bin/echo "ERROR: File could not be installed: ""$LCC_FULLPATH"
        exit 1;
    fi

    /bin/echo "... ready installing LCC components."
    /bin/echo " "
} # installLCCComponents
#########################



####################################
# function installPOSComponents {} #
#                                  #
# Copy new POS components to their #
# correct destination locations    #
####################################
function installPOSComponents {
    # Installing "/Library/Application Support/eLicenser/".
    if [ ! -d "$POS_FULLPATH" ]; then
        # POS file/folder has been deleted successfully in the preflight script,
        # so move the new one from the tmp install location to the final destination.
        /bin/echo "Installing POS components..."
        /bin/mv -f "$TMP_INSTALL_SUBFOLDER_POS""eLicenser" "$DESTINATION_FOLDER_POS""eLicenser";
    else
        # POS file/folder already exists, because it couldn't be deleted in the preflight script.
        /bin/echo "ERROR: Old file could not be removed: ""$DESTINATION_FOLDER_POS""eLicenser"
        exit 1;
    fi


    if [ ! -d "$POS_FULLPATH" ]; then
        # POS file/folder doesn't exist, it hasn't been moved from the tmp location.
        /bin/echo "ERROR: File could not be installed: \"$POS_FULLPATH\"."
        exit 1;
    else
        # POS file/folder exists, installation was successfull.
        /bin/echo "... ready installing POS components."
    fi

    /bin/echo " "
} # installPOSComponents
#########################



####################################
# function installAUSComponents {} #
#                                  #
# Copy new AUS components to their #
# correct destination locations    #
####################################
function installAUSComponents {
    # Installing "ApplicationSupport.plugin".
    /bin/echo "Installing AUS components..."
    
    if [ ! -d "$PATH_TO_AUS_KERNEL_PLUGIN" ]; then
        # AUS file/folder has to be installed for the first time,
        # so move the new one from the tmp install location to the final destination.
        /bin/mv "$TMP_INSTALL_SUBFOLDER_AUS""$PLATFORM_SPECIFIC_AUS_SUB_PATH""/AuthenticationSupport.plugin" "$PATH_TO_AUS_KERNEL_PLUGIN";
    else
        # Check for erroneous 1.0.0.1 plug-in.
        ERRONEOUS_VERSION_PRESENT=0;
        ERRONEOUS_FILE_HASH1="9c4440430d9e5874d50e6f6527142303";
        ERRONEOUS_FILE_HASH2="ab4a272b62022c7433e7fb07b8f75b94";
        ERRONEOUS_FILE_HASH3="ce820824f0476b463faf2c9de79d5c42";
        ERRONEOUS_FILE_HASH4="b94727247929ec321ba5e313df1bfbfa";
        ERRONEOUS_FILE_HASH5="e015785f27f598898b835097518286fb";
        ERRONEOUS_FILE_HASH6="8e3b95fb034a853850a65ed38ee79675";
        CURRENT_HASH=`/sbin/md5 -q "$PATH_TO_AUS_KERNEL_PLUGIN""/Contents/Info.plist"`;
        
        if [ "$CURRENT_HASH" = "$ERRONEOUS_FILE_HASH1" ]; then
            ERRONEOUS_VERSION_PRESENT=1;
        fi
        
        if [ "$CURRENT_HASH" = "$ERRONEOUS_FILE_HASH2" ]; then
            ERRONEOUS_VERSION_PRESENT=1;
        fi
        
        if [ "$CURRENT_HASH" = "$ERRONEOUS_FILE_HASH3" ]; then
            ERRONEOUS_VERSION_PRESENT=1;
        fi
        
        if [ "$CURRENT_HASH" = "$ERRONEOUS_FILE_HASH4" ]; then
            ERRONEOUS_VERSION_PRESENT=1;
        fi
        
        if [ "$CURRENT_HASH" = "$ERRONEOUS_FILE_HASH5" ]; then
            ERRONEOUS_VERSION_PRESENT=1;
        fi
        
        if [ "$CURRENT_HASH" = "$ERRONEOUS_FILE_HASH6" ]; then
            ERRONEOUS_VERSION_PRESENT=1;
        fi
        
        
        if [ $ERRONEOUS_VERSION_PRESENT -eq 1 ]; then
            # We need to upgrade anyway.
            /bin/rm -f "$PATH_TO_AUS_KERNEL_PLUGIN""/Contents/Info.plist"
            /bin/mv "$TMP_INSTALL_SUBFOLDER_AUS""$PLATFORM_SPECIFIC_AUS_SUB_PATH""/AuthenticationSupport.plugin/Contents/Info.plist" "$PATH_TO_AUS_KERNEL_PLUGIN""/Contents/Info.plist";
        fi

        # Do not overwrite existing AUS kernel plug-in, delete version from tmp location instead.
        /bin/rm -rf "$TMP_INSTALL_SUBFOLDER_AUS""$PLATFORM_SPECIFIC_AUS_SUB_PATH_PPC""/AuthenticationSupport.plugin";
        /bin/rm -rf "$TMP_INSTALL_SUBFOLDER_AUS""$PLATFORM_SPECIFIC_AUS_SUB_PATH_I386""/AuthenticationSupport.plugin";
    fi

    if [ ! -d "$PATH_TO_AUS_KERNEL_PLUGIN" ]; then
        # AUS file/folder doesn't exist, it hasn't been moved from the tmp location.
        /bin/echo "ERROR: File could not be installed: $PATH_TO_AUS_KERNEL_PLUGIN"
        exit 1;
    fi


    # Installing "ApplicationSupportEnabler.plugin".
    if [ ! -d "$PATH_TO_AUE_KERNEL_PLUGIN" ]; then
        # AUE file/folder has to be installed for the first time,
        # so move the new one from the tmp install location to the final destination.
        /bin/mv "$TMP_INSTALL_SUBFOLDER_AUS""$PLATFORM_SPECIFIC_AUS_SUB_PATH""/AuthenticationSupportEnabler.plugin" "$PATH_TO_AUE_KERNEL_PLUGIN";
    else
        # Check for erroneous 1.0.0.1 plug-in.
        ERRONEOUS_VERSION_PRESENT=0;
        ERRONEOUS_FILE_HASH="5cc6bc1381b84219f790093d286dc61d";
        CURRENT_HASH=`/usr/bin/grep "xml version" -A48 "$PATH_TO_AUE_KERNEL_PLUGIN""/Contents/Info.plist" | /sbin/md5 -q`;
        
        if [ "$CURRENT_HASH" = "$ERRONEOUS_FILE_HASH" ]; then
            ERRONEOUS_VERSION_PRESENT=1;
        fi
        
        
        if [ $ERRONEOUS_VERSION_PRESENT -eq 1 ]; then
            # We need to upgrade anyway.
            /bin/rm -f "$PATH_TO_AUE_KERNEL_PLUGIN""/Contents/Info.plist"
            /bin/mv "$TMP_INSTALL_SUBFOLDER_AUS""$PLATFORM_SPECIFIC_AUS_SUB_PATH""/AuthenticationSupportEnabler.plugin/Contents/Info.plist" "$PATH_TO_AUE_KERNEL_PLUGIN""/Contents/Info.plist";
        fi

        # Do not overwrite existing AUE kernel plug-in, delete version from tmp location instead.
        /bin/rm -rf "$TMP_INSTALL_SUBFOLDER_AUS""$PLATFORM_SPECIFIC_AUS_SUB_PATH_PPC""/AuthenticationSupportEnabler.plugin";
        /bin/rm -rf "$TMP_INSTALL_SUBFOLDER_AUS""$PLATFORM_SPECIFIC_AUS_SUB_PATH_I386""/AuthenticationSupportEnabler.plugin";
    fi

    if [ ! -d "$PATH_TO_AUS_KERNEL_PLUGIN" ]; then
        # AUE file/folder doesn't exist, it hasn't been moved from the tmp location.
        /bin/echo "ERROR: File could not be installed: $PATH_TO_AUE_KERNEL_PLUGIN"
        exit 1;
    fi


    # Installing kernel plug-in preferences.
    if [ ! -e "$PATH_TO_AUS_KERNEL_PLUGIN_PREFERENCES" ]; then
        # AUS file/folder has to be installed for the first time,
        # so move the new one from the tmp install location to the final destination.
        /bin/mv "$TMP_INSTALL_SUBFOLDER_AUS""$PLATFORM_SPECIFIC_AUS_SUB_PATH""/$AUS_KERNEL_PLUGIN_PREFS_FILE" "$PATH_TO_AUS_KERNEL_PLUGIN_PREFERENCES";
    else
        # Do not overwrite existing AUS kernel plug-in preferences, delete version from tmp location instead.
        /bin/rm -rf "$TMP_INSTALL_SUBFOLDER_AUS""$PLATFORM_SPECIFIC_AUS_SUB_PATH_PPC""/$AUS_KERNEL_PLUGIN_PREFS_FILE";
        /bin/rm -rf "$TMP_INSTALL_SUBFOLDER_AUS""$PLATFORM_SPECIFIC_AUS_SUB_PATH_I386""/$AUS_KERNEL_PLUGIN_PREFS_FILE";
    fi

    if [ ! -e "$PATH_TO_AUS_KERNEL_PLUGIN_PREFERENCES" ]; then
        # AUS file/folder doesn't exist, it hasn't been moved from the tmp location.
        /bin/echo "ERROR: File doesn't exist: $PATH_TO_AUS_KERNEL_PLUGIN_PREFERENCES"
        exit 1;
    fi

    /bin/echo "... ready installing AUS components."
    /bin/echo " "
} # installAUSComponents
#########################



############################################
# function installReceiptsPKGComponents {} #
#                                          #
# Copy legacy receipts PKG files to their  #
# correct destination locations            #
############################################
function installReceiptsPKGComponents {
    # Installing "/Library/Receipts/LCC.pkg".
    /bin/echo "Installing legacy PKG components..."
    if [ ! -d "$RECEIPTS_PKG_LCC_FULLPATH" ]; then
        # LCC.pkg file/folder has been deleted successfully,
        # so move the new one from the tmp install location to the final destination.
        /bin/mv -f "$TMP_INSTALL_SUBFOLDER_PKG/LCC.pkg" "$RECEIPTS_PKG_LCC_FULLPATH";
    else
        # LCC file/folder already exists, because it couldn't be deleted in the preflight script.
        /bin/echo "ERROR: Old file could not be removed: ""$RECEIPTS_PKG_LCC_FULLPATH"
        exit 1;
    fi


    if [ ! -d "$RECEIPTS_PKG_LCC_FULLPATH" ]; then
        # LCC.pkg file/folder doesn't exist, it hasn't been moved from the tmp location.
        /bin/echo "ERROR: File could not be installed: ""$RECEIPTS_PKG_LCC_FULLPATH"
        exit 1;
    fi


    # Installing "/Library/Receipts/NOS.pkg".
    if [ ! -d "$RECEIPTS_PKG_NOS_FULLPATH" ]; then
        # NOS.pkg file/folder has been deleted successfully,
        # so move the new one from the tmp install location to the final destination.
        /bin/mv -f "$TMP_INSTALL_SUBFOLDER_PKG/NOS.pkg" "$RECEIPTS_PKG_NOS_FULLPATH";
    else
        # NOS file/folder already exists, because it couldn't be deleted in the preflight script.
        /bin/echo "ERROR: Old file could not be removed: ""$RECEIPTS_PKG_NOS_FULLPATH"
        exit 1;
    fi


    if [ ! -d "$RECEIPTS_PKG_NOS_FULLPATH" ]; then
        # NOS.pkg file/folder doesn't exist, it hasn't been moved from the tmp location.
        /bin/echo "ERROR: File could not be installed: ""$RECEIPTS_PKG_NOS_FULLPATH"
        exit 1;
    fi


    # Installing "/Library/Receipts/POS.pkg".
    if [ ! -d "$RECEIPTS_PKG_POS_FULLPATH" ]; then
        # POS.pkg file/folder has been deleted successfully,
        # so move the new one from the tmp install location to the final destination.
        /bin/mv -f "$TMP_INSTALL_SUBFOLDER_PKG/POS.pkg" "$RECEIPTS_PKG_POS_FULLPATH";
    else
        # POS file/folder already exists, because it couldn't be deleted in the preflight script.
        /bin/echo "ERROR: Old file could not be removed: ""$RECEIPTS_PKG_POS_FULLPATH"
        exit 1;
    fi


    if [ ! -d "$RECEIPTS_PKG_POS_FULLPATH" ]; then
        # POS.pkg file/folder doesn't exist, it hasn't been moved from the tmp location.
        /bin/echo "ERROR: File could not be installed: ""$RECEIPTS_PKG_POS_FULLPATH"
        exit 1;
    fi


    # Installing "/Library/Receipts/SyncrosoftLicenseControl.pkg".
    if [ ! -d "$RECEIPTS_PKG_SLC_FULLPATH" ]; then
        # SyncrosoftLicenseControl.pkg file/folder has been deleted successfully, 
        # so move the new one from the tmp install location to the final destination.
        /bin/mv -f "$TMP_INSTALL_SUBFOLDER_PKG/SyncrosoftLicenseControl.pkg" "$RECEIPTS_PKG_SLC_FULLPATH";
    else
        # SyncrosoftLicenseControl file/folder already exists, because it couldn't be deleted in the preflight script.
        /bin/echo "ERROR: Old file could not be removed: ""$RECEIPTS_PKG_SLC_FULLPATH"
        exit 1;
    fi


    if [ ! -d "$RECEIPTS_PKG_SLC_FULLPATH" ]; then
        # SyncrosoftLicenseControl.pkg file/folder doesn't exist, it hasn't been moved from the tmp location.
        /bin/echo "ERROR: File could not be installed: ""$RECEIPTS_PKG_SLC_FULLPATH"
        exit 1;
    fi

    /bin/echo "... ready installing legacy PKG components."
    /bin/echo " "
} # installReceiptsPKGComponents
#################################



##############################################
# function installReceiptsPlistComponents {} #
#                                            #
# Copy legacy receipts plist files to their  #
# correct destination locations              #
##############################################
function installReceiptsPlistComponents {
    /bin/echo "Installing legacy plist components..."

    # Installing "/var/db/receipts/com.syncrosoft.installer.licensecontrol.plist".
    if [ ! -e "$RECEIPTS_PLIST_SLC_FULLPATH" ]; then
        # SLC plist file has been deleted successfully,
        # so move the new one from the tmp install location to the final destination.
        /bin/mv -f "$TMP_INSTALL_SUBFOLDER_PKG/com.syncrosoft.installer.licensecontrol.plist" "$RECEIPTS_PLIST_SLC_FULLPATH";
    else
        # SLC plist file already exists, because it couldn't be deleted.
        /bin/echo "ERROR: Old file could not be removed: ""$RECEIPTS_PLIST_SLC_FULLPATH"
        exit 1;
    fi


    if [ ! -e "$RECEIPTS_PLIST_SLC_FULLPATH" ]; then
        # SLC plist file doesn't exist, it hasn't been moved from the tmp location.
        /bin/echo "ERROR: File could not be installed: ""$RECEIPTS_PLIST_SLC_FULLPATH"
        exit 1;
    fi


    # Installing "/var/db/receipts/com.syncrosoft.installer.licensecontrol.bom".
    if [ ! -e "$RECEIPTS_BOM_SLC_FULLPATH" ]; then
        # SLC bom file has been deleted successfully,
        # so create a new one at the final destination.
        /usr/bin/touch "$RECEIPTS_BOM_SLC_FULLPATH";
    else
        # SLC plist file already exists, because it couldn't be deleted.
        /bin/echo "WARNING: Old file could not be removed: ""$RECEIPTS_BOM_SLC_FULLPATH"
    fi


    if [ ! -e "$RECEIPTS_BOM_SLC_FULLPATH" ]; then
        # SLC plist file doesn't exist, it hasn't been moved from the tmp location.
        /bin/echo "ERROR: File could not be installed: ""$RECEIPTS_BOM_SLC_FULLPATH"
        exit 1;
    fi

    /bin/echo "... ready installing PKG components."
    /bin/echo " "
} # installReceiptsPlistComponents
#################################



################################################
# function installReceiptsFiles {}             #
#                                              #
# Copy the legacy receipts files to their      #
# correct destination locations to prevent     #
# older installers from installing their stuff #
################################################
function installReceiptsFiles {
    # Install legacy receipts files either to "/Library/Receipts/" or to "/var/db/receipts/".
    if [ $INSTALLER_SUPPORTS_RECEIPTS_PACKAGES -eq 1 ]; then
        installReceiptsPKGComponents;
    else
        installReceiptsPlistComponents;
    fi
} # installReceiptsFiles
#########################



####################################
# function installLogFolders {}    #
#                                  #
# Create the Log folders at their  #
# correct locations                #
####################################
function installLogFolders {
    # Creating folder "/Library/Logs/eLicenser/".
    /bin/echo "Installing logging folders..."

    # Ensuring that the log folder "/Library/Logs/eLicenser/" exists.
    if [ ! -d "$PATH_TO_GLOBAL_LOG_FOLDER" ]; then
        mkdir -p "$PATH_TO_GLOBAL_LOG_FOLDER";
    fi

    if [ -d "$PATH_TO_GLOBAL_LOG_FOLDER" ]; then
        # Set permission to log folder, so it can receive legacy log files.
        chown -R $USER:admin "$PATH_TO_GLOBAL_LOG_FOLDER";
        chmod -R ugo+rwx     "$PATH_TO_GLOBAL_LOG_FOLDER";
    else
        /bin/echo "ERROR: Folder doesn't exist: $PATH_TO_GLOBAL_LOG_FOLDER";
    fi

    /bin/echo "... ready installing logging folders."
    /bin/echo " "
} # installLogFolders
######################



##############################################
# function setPermissionsForLCCComponents {} #
#                                            #
# Setting ownership & permissions            #
# for LCC components                         #
##############################################
function setPermissionsForLCCComponents {
    /bin/echo "Setting Ownership & Permissions of LCC components..."

    # Setting ownership & permissions for "/Applications/License Control Center.app".
    if [ -d "$LCC_FULLPATH" ]; then
        chown -R $USER:admin "$LCC_FULLPATH"
        chmod -R ugo+rwx,o-w "$LCC_FULLPATH"
    else
        /bin/echo "ERROR: File doesn't exist: $LCC_FULLPATH"
        exit 1;
    fi

    /bin/echo "...ready setting Ownership & Permissions of LCC components."
    /bin/echo " "
} # setPermissionsForLCCComponents
###################################



##############################################
# function setPermissionsForPOSComponents {} #
#                                            #
# Setting ownership & permissions            #
# for POS components                         #
##############################################
function setPermissionsForPOSComponents {
    /bin/echo "Setting Ownership & Permissions of POS components..."

    # Setting ownership & permissions for "/Library/Application Suppport/eLicenser/".
    if [ -d "$POS_FULLPATH" ]; then
        chown -R $USER:admin "$POS_FULLPATH"
        chmod -R ugo+rwx     "$POS_FULLPATH"
        chmod    o-w         "$POS_FULLPATH"
    else
        /bin/echo "ERROR: Folder doesn't exist: $POS_FULLPATH"
        exit 1;
    fi


    # Setting ownership & permissions for components inside "/Library/Application Suppport/eLicenser/pos/".
    if [ -d "$POS_COMPONENTS_PARENT_PATH" ]; then
        chmod -R o-w         "$POS_COMPONENT_POS"
        chmod -R o-w         "$POS_COMPONENT_ACC"
        chmod -R o-w         "$POS_COMPONENT_SEL"
    else
        /bin/echo "ERROR: Folder doesn't exist: $POS_COMPONENTS_PARENT_PATH"
        exit 1;
    fi


    # Setting ownership & permissions for "/Library/Application Suppport/Soft-eLicenser/".
    if [ -d "$SEL_FULLPATH" ]; then
        chown -R $USER:admin "$SEL_FULLPATH"
        chmod -R ugo+rwx     "$SEL_FULLPATH"
    fi

    /bin/echo "...ready setting Ownership & Permissions of POS components."
    /bin/echo " "
} # setPermissionsForPOSComponents
###################################



##############################################
# function setPermissionsForAUSComponents {} #
#                                            #
# Setting ownership & permissions            #
# for AUS components                         #
##############################################
function setPermissionsForAUSComponents {
    /bin/echo "Setting Ownership & Permissions of AUS components..."

    # Setting ownership & permissions for "ApplicationSupport.plugin".
    if [ -d "$PATH_TO_AUS_KERNEL_PLUGIN" ]; then
        chown -R $USER:admin "$PATH_TO_AUS_KERNEL_PLUGIN"
        chmod -R ugo+rxw     "$PATH_TO_AUS_KERNEL_PLUGIN"
        chmod    ugo-w       "$PATH_TO_AUS_KERNEL_PLUGIN"
        chmod -R ugo-w       "$PATH_TO_AUS_KERNEL_PLUGIN""/Contents/MacOS/"
    else
        /bin/echo "ERROR: File doesn't exist: $PATH_TO_AUS_KERNEL_PLUGIN"
        exit 1;
    fi


    # Setting ownership & permissions for "ApplicationSupportEnabler.plugin".
    if [ -d "$PATH_TO_AUE_KERNEL_PLUGIN" ]; then
        chown -R $USER:admin "$PATH_TO_AUE_KERNEL_PLUGIN"
        chmod -R ugo+rxw     "$PATH_TO_AUE_KERNEL_PLUGIN"
        chmod    ugo-w       "$PATH_TO_AUE_KERNEL_PLUGIN"
        chmod -R ugo-w       "$PATH_TO_AUE_KERNEL_PLUGIN""/Contents/MacOS/"
    else
        /bin/echo "ERROR: File doesn't exist: $PATH_TO_AUE_KERNEL_PLUGIN"
        exit 1;
    fi


    # Setting ownership & permissions for kernel plug-in preferences.
    if [ -e "$PATH_TO_AUS_KERNEL_PLUGIN_PREFERENCES" ]; then
        chown -R $USER:admin "$PATH_TO_AUS_KERNEL_PLUGIN_PREFERENCES"
        chmod -R ugo+rwx     "$PATH_TO_AUS_KERNEL_PLUGIN_PREFERENCES"
    else
        /bin/echo "ERROR: File doesn't exist: $PATH_TO_AUS_KERNEL_PLUGIN_PREFERENCES"
        exit 1;
    fi

    /bin/echo "...ready setting Ownership & Permissions of AUS components."
    /bin/echo " "
} # setPermissionsForAUSComponents
###################################



######################################################
# function setPermissionsForReceiptsPKGComponents {} #
#                                                    #
# Setting ownership & permissions for legacy PKG     #
# components                                         #
######################################################
function setPermissionsForReceiptsPKGComponents {
    /bin/echo "Setting Ownership & Permissions of legacy PKG components..."

    # Setting ownership & permissions for legacy "/Library/Receipts/LCC.pkg" file.
    if [ -d "$RECEIPTS_PKG_LCC_FULLPATH" ]; then
        chown -R root:admin  "$RECEIPTS_PKG_LCC_FULLPATH"
        chmod -R ugo+rxw     "$RECEIPTS_PKG_LCC_FULLPATH"
        chmod -R o-w         "$RECEIPTS_PKG_LCC_FULLPATH"
    else
        /bin/echo "ERROR: File doesn't exist: $RECEIPTS_PKG_LCC_FULLPATH"
        exit 1;
    fi


    # Setting ownership & permissions for legacy "/Library/Receipts/NOS.pkg" file.
    if [ -d "$RECEIPTS_PKG_NOS_FULLPATH" ]; then
        chown -R root:admin  "$RECEIPTS_PKG_NOS_FULLPATH"
        chmod -R ugo+rxw     "$RECEIPTS_PKG_NOS_FULLPATH"
        chmod -R o-w         "$RECEIPTS_PKG_NOS_FULLPATH"
    else
        /bin/echo "ERROR: File doesn't exist: $RECEIPTS_PKG_NOS_FULLPATH"
        exit 1;
    fi


    # Setting ownership & permissions for legacy "/Library/Receipts/POS.pkg" file.
    if [ -d "$RECEIPTS_PKG_POS_FULLPATH" ]; then
        chown -R root:admin  "$RECEIPTS_PKG_POS_FULLPATH"
        chmod -R ugo+rxw     "$RECEIPTS_PKG_POS_FULLPATH"
        chmod -R o-w         "$RECEIPTS_PKG_POS_FULLPATH"
    else
        /bin/echo "ERROR: File doesn't exist: $RECEIPTS_PKG_POS_FULLPATH"
        exit 1;
    fi


    # Setting ownership & permissions for legacy "/Library/Receipts/SyncrosoftLicenseControl.pkg" file.
    if [ -d "$RECEIPTS_PKG_SLC_FULLPATH" ]; then
        chown -R root:admin  "$RECEIPTS_PKG_SLC_FULLPATH"
        chmod -R ugo+rxw     "$RECEIPTS_PKG_SLC_FULLPATH"
        chmod -R o-w         "$RECEIPTS_PKG_SLC_FULLPATH"
    else
        /bin/echo "ERROR: File doesn't exist: $RECEIPTS_PKG_SLC_FULLPATH"
        exit 1;
    fi

    /bin/echo "...ready setting Ownership & Permissions of legacy PKG components."
    /bin/echo " "
} # setPermissionsForReceiptsPKGComponents
###########################################



########################################################
# function setPermissionsForReceiptsPlistComponents {} #
#                                                      #
# Setting ownership & permissions for legacy plist     #
# components                                           #
########################################################
function setPermissionsForReceiptsPlistComponents {
    /bin/echo "Setting Ownership & Permissions of legacy plist files..."

    # Setting ownership & permissions for legacy file "/var/db/receipts/com.syncrosoft.installer.licensecontrol.plist".
    if [ -e "$RECEIPTS_PLIST_SLC_FULLPATH" ]; then
        chown root:wheel  "$RECEIPTS_PLIST_SLC_FULLPATH"
        chmod ugo+r       "$RECEIPTS_PLIST_SLC_FULLPATH"
        chmod u+w         "$RECEIPTS_PLIST_SLC_FULLPATH"
        chmod go-xw       "$RECEIPTS_PLIST_SLC_FULLPATH"
    else
        /bin/echo "ERROR: File doesn't exist: $RECEIPTS_PLIST_SLC_FULLPATH"
        exit 1;
    fi


    # Setting ownership & permissions for legacy file "/var/db/receipts/com.syncrosoft.installer.licensecontrol.bom".
    if [ -e "$RECEIPTS_BOM_SLC_FULLPATH" ]; then
        chown root:wheel  "$RECEIPTS_BOM_SLC_FULLPATH"
        chmod ugo+r       "$RECEIPTS_BOM_SLC_FULLPATH"
        chmod u+w         "$RECEIPTS_BOM_SLC_FULLPATH"
        chmod go-xw       "$RECEIPTS_BOM_SLC_FULLPATH"
    else
        /bin/echo "ERROR: File doesn't exist: $RECEIPTS_BOM_SLC_FULLPATH"
        exit 1;
    fi

    /bin/echo "...ready setting Ownership & Permissions of leagcy plist files."
    /bin/echo " "
} # setPermissionsForReceiptsPlistComponents
#############################################



##############################################
# function setPermissionsForReceiptsFiles {} #
#                                            #
# Setting ownership & permissions            #
# for legacy receipts files                  #
##############################################
function setPermissionsForReceiptsFiles {
    # Set ownership & permissions of legacy receipts files 
    # either in "/Library/Receipts/" or in "/var/db/receipts/".
    if [ $INSTALLER_SUPPORTS_RECEIPTS_PACKAGES -eq 1 ]; then
        setPermissionsForReceiptsPKGComponents;
    else
        setPermissionsForReceiptsPlistComponents;
    fi
} # setPermissionsForReceiptsFiles
###################################



###########################################
# function setPermissionsForLogFolders {} #
#                                         #
# Setting ownership & permissions         #
# for log folders                         #
###########################################
function setPermissionsForLogFolders {
    /bin/echo "Setting Ownership & Permissions of log folders..."

    # Setting ownership & permissions either for the log folder "/Library/Logs/eLicenser/eLCC/",
    # and make sure it exists in case it hasn't been created yet.
    if [ ! -d "$PATH_TO_GLOBAL_LOG_FOLDER" ]; then
        mkdir -p "$PATH_TO_GLOBAL_LOG_FOLDER"
    fi

    if [ -d "$PATH_TO_GLOBAL_LOG_FOLDER" ]; then
        chown -R $USER:admin "$PATH_TO_GLOBAL_LOG_FOLDER";
        chmod -R ugo+rwx     "$PATH_TO_GLOBAL_LOG_FOLDER";
    else
        /bin/echo "ERROR: Folder doesn't exist: $PATH_TO_GLOBAL_LOG_FOLDER";
        exit 1;
    fi

    /bin/echo "...ready setting Ownership & Permissions of log folders.";
    /bin/echo " "
} # setPermissionsForLogFolders
################################



##############################################
# function createLegacySupport {}            #
#                                            #
# Create the symlinks etc. necessary for old #
# SLC installers to find the eLC components  #
##############################################
function createLegacySupport {
    /bin/echo "Creating legacy support files..."

    # Create POS symlink.
    if [ ! -e "$POS_SYMLINK_TARGET" ]; then
        if [ ! -e "$POS_PARENT_FOLDER_OF_SYMLINK_TARGET" ]; then
            mkdir -p "$POS_PARENT_FOLDER_OF_SYMLINK_TARGET";
        fi

        if [ -d "$POS_PARENT_FOLDER_OF_SYMLINK_TARGET" ]; then
            ln -s "$POS_SYMLINK_SOURCE" "$POS_SYMLINK_TARGET";
        fi
    fi

    if [ ! -L "$POS_SYMLINK_TARGET" ]; then
        /bin/echo "ERROR: Symbolic link doesn't exist: $POS_SYMLINK_TARGET";
        exit 1;
    else
        chown -R $USER:admin "$POS_PARENT_FOLDER_OF_SYMLINK_TARGET";
        chmod -R ugo+rx      "$POS_PARENT_FOLDER_OF_SYMLINK_TARGET";
    fi

    /bin/echo "...ready creating legacy support files."
    /bin/echo " "
} # createLegacySupport
########################



################################
# function migrateLogFiles {}  #
#                              #
# Migrate useful existing Log  #
# files to the new location    #
###############################
function migrateLogFiles {
    # Creating folder "/Library/Logs/eLicenser/eLCC/".
    /bin/echo "Migrating log files..."

    if [ -d "$PATH_TO_LEGACY_USER_LOG_FOLDER" ]; then
        # Ensure we have write permission in the new log folder locations.
        setPermissionsForLogFolders;
        
        # Moving existing log files from legacy location to new location.
        /usr/bin/ditto "$PATH_TO_LEGACY_USER_LOG_FOLDER" "$PATH_TO_GLOBAL_LOG_FOLDER";
        if [ $? -ne 0 ]; then
            /bin/echo "WARNING: couldn't migrate log files from '$PATH_TO_LEGACY_USER_LOG_FOLDER' to '$PATH_TO_GLOBAL_LOG_FOLDER'...";
        fi
    fi
    
    # After migrating the legacy log files, let's delete the remaining empty folders.
    if [ -d "$PATH_TO_LEGACY_USER_LOG_PARRENT_FOLDER" ]; then
        /bin/rm -fR "$PATH_TO_LEGACY_USER_LOG_PARRENT_FOLDER";
    fi

    if [ -e "$PATH_TO_LEGACY_USER_LOG_PARRENT_FOLDER" ]; then
        /bin/echo "WARNING: Folder could not be removed: ""$PATH_TO_LEGACY_USER_LOG_PARRENT_FOLDER";
    fi


    if [ -d "$PATH_TO_LEGACY_GLOBAL_LOG_PARRENT_FOLDER" ]; then
        /bin/rm -fR "$PATH_TO_LEGACY_GLOBAL_LOG_PARRENT_FOLDER";
    fi

    if [ -e "$PATH_TO_LEGACY_GLOBAL_LOG_PARRENT_FOLDER" ]; then
        /bin/echo "WARNING: Folder could not be removed: ""$PATH_TO_LEGACY_GLOBAL_LOG_PARRENT_FOLDER";
    fi

    # Within 'removeOldComponents()', any exisiting SeL log file had been backed up using the
    # 'migrate_SeL_LogFile_prepare()' function, to prevent its deletion while the enclosing 
    # "Syncrosoft" folder got deleted. 
    # Now let's call the 'migrate_SeL_LogFile_finish()' function to ensure that any backed up 
    # SeL log file gets restored to its new location.
    migrate_SeL_LogFile_finish;

    /bin/echo "... ready installing logging folders."
    /bin/echo " "
} # migrateLogFiles
######################



###########################################
# function registerPOSAtLaunchServices {} #
#                                         #
# Register Synsopos.app at LaunchServices #
# to prefer native environment            #
###########################################
function registerPOSAtLaunchServices {
    # Calling LaunchServices to ensure Synsopos' Finder Info is *not* set to 'Open using Rosetta'.
    if [ -e "$LAUNCHSERVICES" ]; then
        /bin/echo "Calling LaunchServices to ensure Synsopos is *not* set to 'Open using Rosetta'."

        "$LAUNCHSERVICES" -f -v "$POS_COMPONENT_POS"

        /bin/echo "...ready calling LaunchServices."
    fi
} # registerPOSAtLaunchServices
################################



###############################
# function cleanup {}         #
#                             #
# Clean up installation files #
###############################
function cleanup {
    # Delete the $TMP_INSTALL_FOLDER
    /bin/echo "Cleaning up installation..."

    /bin/rm -rf "$TMP_INSTALL_SUBFOLDER_AUS";
    /bin/rm -rf "$TMP_INSTALL_SUBFOLDER_LCC";
    /bin/rm -rf "$TMP_INSTALL_SUBFOLDER_POS";
    /bin/rm -rf "$TMP_INSTALL_SUBFOLDER_PKG";

    /bin/echo "... ready cleaning up installation."

    # Don't remove the installation folder itself
    # lest Apple's Installer application fails.
    #rmdir  "$TMP_INSTALL_FOLDER";
} # cleanup
############



############################
# function launchLCC {}    #
#                          #
# Conditionally launch LCC #
############################
function launchLCC {
    if [ -e "$PATH_TO_LCC_LAUNCHER" ]; then
        if [ $SN_UPDATE_REQUIRED -eq 1 ]; then
            # Configure LCC to show dialog for fixed PPC SeL.
            /usr/bin/touch "$PATH_TO_LCC_LAUNCHER_FLAG_FILE";

            # Launch helper application.
            /usr/bin/open "$PATH_TO_LCC_LAUNCHER";
        fi
    fi
} # launchLCC
##############



#############
# Main code #
#############

/bin/echo "Running postinstall script..."

# Set up all the path variables used during script execution.
setupPaths "$2";

# Determine whether the current hardware is an Intel-Mac or a PPC-Mac.
checkPlatform;

# Check whether one of the early erroneous Soft-eLicenser versions had been installed and 
# needs to be fixed.
checkSeLVersion;

# Determine the way the current operating system handles receipts files to indicate 
# previously installed versions of the license control software.
setReceiptsHandling;

# Remove any previously installed files to ensure a "clean" installation 
# (existing Soft-eLicenser storage files are preserved, though).
removeOldComponents;

# Install all the current components.
installLCCComponents;
installPOSComponents;
installAUSComponents;

# Install appropriate receipts files to prevent older license control installers 
# from overwriting this installation.
installReceiptsFiles;

# Create appropriate logging folders to allow the license control components to write logging data.
installLogFolders;

# Move any eLC logging files wich still exist at the deprecated SLC logging locations 
# over to the currently correct eLC logging location.
migrateLogFiles;

# Set ownership and permissions of all installed files.
setPermissionsForLCCComponents;
setPermissionsForPOSComponents;
setPermissionsForAUSComponents;
setPermissionsForReceiptsFiles;
setPermissionsForLogFolders;

# Ensure that customized application installers which rely on the existence of the 
# deprecated SLC installation are still able to access the Synsopos via command line.
createLegacySupport;

# Ensure that Synsopos is always started natively (to prevent launching via Rosetta emulation).
registerPOSAtLaunchServices;

# Remove the temporary installation paths.
cleanup;

# Allow eLCC to be get launched in case the Soft-eLicenser needs immediate repairing.
launchLCC;

/bin/echo "... finished running postinstall script."

exit 0;
